
package cn.jzx.scalademo1

object HelloWorld extends App {

  /*    //1.
      println("scala is good ..")*/


  /*    //2.
      var n = 5
      n = n + 4
      println(n)*/


  /*  //3.
    //变量定义
    //java type 变量名 = 值;
    //var|bal bi 变量名:type=值
    var n = 5 //  类型推导	n 是Int 类型，是通过5这个值推导得出
    var m: Int = 7 //  声明时指明数据类型
    val k = 9 //  val 表示 k 是一个常量
    //java.sql.Date sqlDate   java.util.Date utilDate
    type SqlDate = java.sql.Date
    type UtilDate = java.util.Date

    var d: UtilDate = new UtilDate()*/


  /*  //4.
    //scala  方法调用可以不用点 用空格  参数参数传递可以不用小括号 用空格
    1 to 10 foreach println
    1.to(10).foreach(i => println(i))*/


  /*  //5.
      for(i <- 1 to 10 ; if i % 2 == 0 ; if i <= 5){
        println(i)
      }
    1 to 10 filter(_ <= 5) filter(_%2==0) foreach println*/


  /*  //6.
    //yield
    //定义一个数组或者集合
    var nn = for (i <- 1 to 10; if i % 2 == 0; if i <= 5) yield i
    // 把符合条件的数据加入到数组或集合中 println(i)
    nn foreach println
    nn foreach println*/


  /*  //7.
    var num = 6
    // 类似shell
    for (i <- 0 to 6) {
      println(s"$i + ${num - i} = $num")
    }

    0 to num map{ i => s"$i + ${num - i} = $num" } foreach println*/


  /*  //8.
    var dd = Array.range(1,10,2)
    dd map{i => i*i} foreach println*/


  /*  //9.
    val yuanzu = (12, "abc", 3.5, 99, 77)
    println(yuanzu.getClass)
    println(yuanzu._2)
    val (a, _, _, _, c) = yuanzu
    println(a)
    // println(b)
    println(c)
    yuanzu.productIterator.foreach(println)*/


  /*  //10.
    var a = scala.collection.mutable.ListBuffer(3, 5, 8)
    a += 77
    //a(1) = 7
    a foreach println*/


  /*  //11.
    //  var c = List[Char]('a', 'b', 'c')
    //  var x = 'x' +: c // (x, a, b, c)
    //  var y = c :+ 'x' // (a, b, c, x)
    //  println(y)

    import scala.collection.mutable._

    var lb = ListBuffer[Int](1, 2, 3, 4)
    lb.+=(100) //1, 2, 3, 4, 100
    lb.+=(21, 33, 44, 55, 66) // ...args
    88 +=: lb //88, 1, 2, 3, 4, 100, 21, 33
    lb ++= List(77, 66)
    println(lb)*/


  /*  //12.
    var l = List[Int](2, 3, 4, 6, 8, 9, 11, 20)
    //  var x = l grouped 3 //等价l.grouped(3)
    //  while (x.hasNext) {
    //    println(x.next())
    //  }

    //  x.next() //(2, 3, 4)
    //  x.next() //(6, 8, 9)
    var y = l sliding 2
    y foreach println
    //  y.next() // (2, 3)
    //  y.next() // (3, 4)*/


/*  //13.
  var a1 = Array[Int](1, 2, 3, 3, 4)
  var a2 = Array[Int](100, 200, 300, 400)
  var a = List.concat(a1, a2) //Array->List
  var a3 = a1 ++ a2 //Arrays->Array

  //  a(3) = 333 //Error
  //  a3(3) = 333 //OK
  //  println(a3.toList)
  //  var b = Array(333, "33333", '3', false)
  //  println(b.toList)
  //  var c = List.concat(a, b)
  //  val x = a.filter(_ % 2 != 0)
  //  println(x)
  //  val y = a.reverse
  //  println(y)
  //  var m = a.groupBy(t => t % 2 == 0)
  //  println(m)
  //  var n = a.slice(2, 4)
  //  println(n)
  //  a.sorted   //1, 2, 3, 4, 100, 200, 300, 400
  //  println(a)
  //  implicit val r = Ordering.Int.reverse
  //  a.sorted
  //  println(a.sorted)
  //  a.sorted( Ordering.Int.reverse )
  //  println(a.sortWith(_ > _))
  //  a.sortBy(x => x) //升序
  //  println(a)
  //  a.sortBy(x => x * (-1)) //降序
  //  println(a)*/



    val a = Array("a" -> 4,"b" -> 3, "c" -> 2)
     println(a.toList)

     println(a.sortBy(_._2).toList)


}
